package tables

import (
	"gitee.com/theegg/go-brick/app/model/commerce/brands"
	"gitee.com/theegg/go-brick/app/model/commerce/goods"
	"gitee.com/theegg/go-brick/app/model/commerce/goods_category"
	"gitee.com/theegg/go-brick/app/model/commerce/goods_set"
	"gitee.com/theegg/go-brick/app/model/commerce/merchant_admin_users"
	"gitee.com/theegg/go-brick/app/model/commerce/merchants"
	"gitee.com/theegg/go-brick/app/model/commerce/orders"
	"gitee.com/theegg/go-brick/app/model/content/node_category"
	"gitee.com/theegg/go-brick/app/model/content/node_comments"
	"gitee.com/theegg/go-brick/app/model/content/nodes"
	"gitee.com/theegg/go-brick/app/model/content/tag_nodes"
	"gitee.com/theegg/go-brick/app/model/content/tags"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_menus"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_permissions"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_role_menus"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_role_permissions"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_role_users"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_roles"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_user_permissions"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_users"
	"gitee.com/theegg/go-brick/app/model/property/property_elements"
	"gitee.com/theegg/go-brick/app/model/secondhand/sh_goods"
	"gitee.com/theegg/go-brick/app/model/system/system_configures"
	"gitee.com/theegg/go-brick/app/model/system/system_districts"
	"gitee.com/theegg/go-brick/app/model/system/system_images"
	"gitee.com/theegg/go-brick/app/model/system/system_modules"
	"gitee.com/theegg/go-brick/app/model/urban/urban_elements"
	"gitee.com/theegg/go-brick/app/model/user/user_account_flow_reports"
	"gitee.com/theegg/go-brick/app/model/user/user_accounts"
	"gitee.com/theegg/go-brick/app/model/user/user_logs"
	"gitee.com/theegg/go-brick/app/model/user/user_permissions"
	"gitee.com/theegg/go-brick/app/model/user/user_role_permissions"
	"gitee.com/theegg/go-brick/app/model/user/user_roles"
	"gitee.com/theegg/go-brick/app/model/user/user_tree"
	"gitee.com/theegg/go-brick/app/model/user/users"
	"gitee.com/theegg/go-brick/app/service/app"
)

var PartnerTableNames = map[string]interface{}{
	"partner_admin_role_menus":       partner_admin_role_menus.PartnerAdminRoleMenus{},
	"partner_admin_menus":            partner_admin_menus.PartnerAdminMenus{},
	"partner_admin_permissions":      partner_admin_permissions.PartnerAdminPermissions{},
	"partner_admin_role_permissions": partner_admin_role_permissions.PartnerAdminRolePermissions{},
	"partner_admin_user_permissions": partner_admin_user_permissions.PartnerAdminUserPermissions{},
	"partner_admin_role_users":       partner_admin_role_users.PartnerAdminRoleUsers{},
	"partner_admin_roles":            partner_admin_roles.PartnerAdminRoles{},
	"partner_admin_users":            partner_admin_users.PartnerAdminUsers{},

	// 用户管理
	"users":                    users.Users{},
	"user_roles":               user_roles.UserRoles{},
	"user_permissions":         user_permissions.UserPermissions{},
	"user_role_permissions":    user_role_permissions.UserRolePermissions{},
	"user_tree":                user_tree.UserTree{},
	"user_logs":                user_logs.UserLogs{},
	"user_accounts":            user_accounts.UserAccounts{},
	"user_account_flow_report": user_account_flow_reports.UserAccountFlowReports{},

	// 文章管理
	"nodes":         nodes.Nodes{},
	"node_comments": node_comments.NodeComments{},
	"node_category": node_category.NodeCategory{},
	"tags":          tags.Tags{},
	"tag_nodes":     tag_nodes.TagNodes{},

	// 商户管理
	"orders":               orders.Orders{},
	"merchants":            merchants.Merchants{},
	"merchant_admin_users": merchant_admin_users.MerchantAdminUsers{},
	"goods_set":            goods_set.GoodsSet{},
	"goods":                goods.Goods{},
	"goods_category":       goods_category.GoodsCategory{},
	"brands":               brands.Brands{},

	// 二手物品
	"secondhand_goods": sh_goods.SecondhandGoods{},

	// 城市管理
	"urban_elements": urban_elements.UrbanElements{},

	// 租售
	"property_elements": property_elements.PropertyElements{},

	// system
	"system_district":   system_districts.SystemDistricts{},
	"system_modules":    system_modules.SystemModules{},
	"system_images":     system_images.SystemImages{},
	"system_configures": system_configures.SystemConfigures{},
}

// @title GenPartnerTable
// @description 生成租户表
func GenPartnerTable() ([]map[string]interface{}, bool) {

	finished := true
	messageList := make([]map[string]interface{}, 0)

	for tableName, value := range PartnerTableNames {
		msg := map[string]interface{}{
			"table":               tableName,
			"table_status":        0,
			"table_del_status":    0,
			"table_create_status": 0,
			"table_create_error":  "",
		}

		// 删除
		if ok, _ := app.Db().IsTableExist(value); !ok {
			msg["table_status"] = 0
		} else {
			msg["table_status"] = 1
			errDrop := app.Db().DropTables(value)
			if errDrop != nil {
				finished = false
				continue
			}
			msg["table_del_status"] = 1

		}

		// 创建
		err := app.Db().Sync2(value)
		if err != nil {
			msg["table_create_error"] = err.Error()
			finished = false
			continue
		}
		msg["table_create_status"] = 1
	}

	return messageList, finished
}
